//
//  HHWCustomePicker.swift
//  Pickers
//
//  Created by 鲁万财 on 15/7/28.
//  Copyright (c) 2015年 鲁万财. All rights reserved.
//

import UIKit
import AudioToolbox

class HHWCustomPicker: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
    
    private var images: Array<UIImage>!;
    private var winSoundID: SystemSoundID = 0;
    private var crunchSoundID: SystemSoundID = 0;
    
    @IBOutlet weak var customPicker: UIPickerView!;
    @IBOutlet weak var button: UIButton!;
    @IBOutlet weak var winLabel: UILabel!;
    
    override func viewDidLoad() {
        super.viewDidLoad();
        
        self.images = [];
        self.images.append(UIImage(named: "seven")!);
        self.images.append(UIImage(named: "bar")!);
        self.images.append(UIImage(named: "crown")!);
        self.images.append(UIImage(named: "cherry")!);
        self.images.append(UIImage(named: "lemon")!);
        self.images.append(UIImage(named: "apple")!);
        
//        srandom(time(NULL));
    }
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 5;
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.images.count;
    }
    
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
        var img = self.images[row];
        var imageView = UIImageView(image: img);
        return imageView;
    }
    
    func showButton() {
        self.button.hidden = false;
    }
    
    func playWinSound() {
        if(winSoundID == 0) {
            var soundURL = NSBundle.mainBundle().URLForResource("win", withExtension: "wav");
//            AudioServicesCreateSystemSoundID(soundURL, &winSoundID);
        }
//        AudioServicesPlayAlertSound(winSoundID);
        self.winLabel.text = "WINNING!";
        self.showButton();
    }
    
    @IBAction func spin(sender: AnyObject) {
        var win = false;
        var numInRow = 1;
        var lastVal = -1;
        for(var i = 0; i < 5; i++) {
            var newValue = random() % self.images.count
            if(newValue == lastVal) {
                numInRow++;
            } else {
                numInRow = 1;
            }
            lastVal = newValue;
            
            self.customPicker.selectRow(newValue, inComponent: i, animated: true);
            self.customPicker.reloadComponent(i);
            if(numInRow >= 3) {
                win = true;
            }
        }
        
        if(crunchSoundID == 0) {
            var path = NSBundle.mainBundle().pathForResource("crunch", ofType: "wav");
            var soundURL = NSURL.fileURLWithPath(path!);
//            AudioServicesCreateSystemSoundID(soundURL, &crunchSoundID);
        }
        
//        AudioServicesPlayAlertSound(crunchSoundID);
        
//        if(win) {
//            self.playWinSound();
//        } else {
//            self.showButton();
//        }
//        
//        self.button.hidden = true;
//        self.winLabel.text = "";
        
        if(win) {
            self.winLabel.text = "WINNING!";
        } else {
            self.winLabel.text = "";
        }
    }
}
